/**
 * Created by xinhuang on 15/6/2.
 */
'use strict';

describe('单元测试: 重大活动实时展示页面管理 服务', function () {
  beforeEach(angular.mock.module('services.customActivity.realtime'));

  var $httpBackend, activityList, activityManager, formControl; // services
  var activities, configModules, newActivity, savedConfig; //respond data
  beforeEach(inject(function ($injector) {
    $httpBackend = $injector.get('$httpBackend');
    activityList = $injector.get('activityList');
    activityManager = $injector.get('activityManager');
    formControl = $injector.get('formControl');

    //活动列表
    activities = {
      data: [{
        activityId: 111,
        activityName: '双十一',
        activityTime: '2015年11月9日——2015年11月11日',
        activityProvince: ['广东', '北京', '上海'],
        activityState: 0
      },
        {
          activityId: 112,
          activityName: '国庆',
          activityTime: '2015年10月1日——2015年10月7日',
          activityProvince: ['全国31省'],
          activityState: 0
        }
      ]
    };

    //功能模块列表
    configModules = {
      data: [{
        moduleName: '业务量',
        moduleId: 11,
        childModule: [
          {
            moduleName: '业务量',
            moduleId: 111,
            childModule: []
          }
        ]
      }]
    };

    //新增活动
    newActivity = {
      activityName: '双十一',
      selectedModules: [111],
      provinceCode: ['111'],
      startTime: '2015-11-11 00:00:00',
      endTime: '2015-11-12 00:00:00',
      displaySetting: {
        time: 0,
        province: 0
      },
      layout: '<div></div>'
    };

    //已保存的配置信息
    savedConfig = {
      data: {
        selectedModules: [111],
        provinceCode: ['111'],
        startTime: '2015-11-11 00:00:00',
        endTime: '2015-11-12 00:00:00',
        displaySetting: {
          time: 0,
          province: 0
        },
        layout: '<div></div>'
      }
    };

    //模拟服务器Response
    $httpBackend.whenGET('/sas/customActivity/realTime/activityList/query').respond(activities);
    $httpBackend.whenGET('/sas/customActivity/realTime/configModules/query').respond(configModules);
  }));

  afterEach(function () {
    $httpBackend.verifyNoOutstandingExpectation();
    $httpBackend.verifyNoOutstandingRequest();
  });

  describe('activityList 服务', function () {
    it('应该能够获取活动列表', function () {
      expect(activityList.activities.length).toBe(0);

      activityList.getList();
      $httpBackend.flush();

      expect(activityList.activities.length).toBe(2);
      expect(activityList.activities[0]).toEqual(activities.data[0]);
    });

    it('应该能够删除活动', function () {
      activityList.activities = activities.data;
      activityList.deleteActivity(111);

      $httpBackend.expectPOST('/sas/customActivity/realTime/activityList/delete', {activityId: 111}).respond({state: '10000'});

      $httpBackend.flush();
    });
  });

  xdescribe('activityManager 服务', function () {

    it('应该可以获取功能模块列表', function () {
      activityManager.getConfigModules();

      $httpBackend.flush();

      expect(activityManager.configModules).toEqual(configModules.data);
    });

    it('应该能够新增活动', function () {
      activityManager.configure = newActivity;
      activityManager.saveActivity();
      $httpBackend.expectPOST('/sas/customActivity/realTime/activity/save', newActivity).respond();
      $httpBackend.flush();
    });

    it('修改活动时应该能够获取已保存在服务器端的配置信息', function () {
      activityManager.getSavedConfig(111);

      $httpBackend.expectPOST('/sas/customActivity/realTime/savedConfig/query', {activityId: 111}).respond(savedConfig);

      $httpBackend.flush();

      expect(activityManager.configure).toEqual(savedConfig.data);
      expect(activityManager.startTime).toEqual(new Date('2015-11-11 00:00:00'));
      expect(activityManager.endTime).toEqual(new Date('2015-11-12 00:00:00'));
    });

    it('应该可以保存修改的活动', function () {
      activityManager.configure = savedConfig;

      activityManager.saveActivity(111);

      $httpBackend.expectPOST('/sas/customActivity/realTime/activity/update').respond();

      $httpBackend.flush();
    });
  });

  describe('formControl 服务', function () {
    it('字符串格式时间和时间对象之间转换', function () {
      expect(formControl.timeTransfor('2015-11-11 00:00:00')).toEqual(new Date('2015-11-11 00:00:00'));
      expect(formControl.timeTransfor(new Date('2015-11-11 00:00:00'))).toEqual('2015-11-11 00:00:00');
    });

    it('可以根据配置的已选模块勾选表单模块', function () {
      var modules = angular.copy(configModules.data);
      formControl.selectModule(modules);
      expect(modules[0].selected).toBeDefined();

      formControl.selectModule(modules, [111]);
      expect(modules[0].childModule[0].selected).toBeTruthy();
    });

    it('应该可以获取被勾选的模块ID', function () {
      var modules = angular.copy(configModules.data);
      modules[0].selected = true;
      modules[0].childModule[0].selected = true;
      expect(formControl.getSelectedModuleId(modules)).toEqual([111]);
    });
  });
});